﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace Lilium.Web.Mvc.Html
{
	public static class JavaScriptString
	{
		public static string QuoteString(string value)
		{
			StringBuilder builder = null;
			if (string.IsNullOrEmpty(value))
			{
				return string.Empty;
			}
			int startIndex = 0;
			int count = 0;
			for (int i = 0; i < value.Length; i++)
			{
				char c = value[i];
				if ((((c == '\r') || (c == '\t')) || ((c == '"') || (c == '\''))) || ((((c == '<') || (c == '>')) || ((c == '\\') || (c == '\n'))) || (((c == '\b') || (c == '\f')) || (c < ' '))))
				{
					if (builder == null)
					{
						builder = new StringBuilder(value.Length + 5);
					}
					if (count > 0)
					{
						builder.Append(value, startIndex, count);
					}
					startIndex = i + 1;
					count = 0;
				}
				switch (c)
				{
					case '<':
					case '>':
					case '\'':
						{
							AppendCharAsUnicode(builder, c);
							continue;
						}
					case '\\':
						{
							builder.Append(@"\\");
							continue;
						}
					case '\b':
						{
							builder.Append(@"\b");
							continue;
						}
					case '\t':
						{
							builder.Append(@"\t");
							continue;
						}
					case '\n':
						{
							builder.Append(@"\n");
							continue;
						}
					case '\f':
						{
							builder.Append(@"\f");
							continue;
						}
					case '\r':
						{
							builder.Append(@"\r");
							continue;
						}
					case '"':
						{
							builder.Append("\\\"");
							continue;
						}
				}
				if (c < ' ')
				{
					AppendCharAsUnicode(builder, c);
				}
				else
				{
					count++;
				}
			}
			if (builder == null)
			{
				return value;
			}
			if (count > 0)
			{
				builder.Append(value, startIndex, count);
			}
			return builder.ToString();
		}

		private static void AppendCharAsUnicode(StringBuilder builder, char c)
		{
			builder.Append(@"\u");
			builder.AppendFormat(CultureInfo.InvariantCulture, "{0:x4}", new object[] { (int)c });
		}
	}
}
